<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['travel:client:add']">新增</el-button>
      </el-col>
<!--      visible.sync="open" 表示：-->
<!--      当 open === true，对话框出现；-->
<!--      当用户点右上角的 X 或“取消”按钮，Element-UI 会自动把 open 设为 false，对话框关闭。-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
<!--.sync修饰父子之间双向绑定-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" style="margin-top: 15px;">
      <el-form-item label="姓名" prop="clientName">
        <el-input v-model="queryParams.clientName" placeholder="请输入客户姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="clientTel">
        <el-input v-model="queryParams.clientTel" placeholder="请输入客户手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户来源" prop="clientSource">
        <el-select v-model="queryParams.clientSource" placeholder="请选择客户来源" clearable>
          <el-option v-for="dict in dict.type.travel_client_source" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">查询</el-button>
         <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="clientList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="序号" align="center" prop="clientId" />-->
      <el-table-column label="序号" align="center">
        <template slot-scope="scope">
          <!-- 公式：(当前页-1) * 每页条数 + 当前行索引 + 1 -->
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="客户姓名" align="center" prop="clientName" />
      <el-table-column label="客户手机号" align="center" prop="clientTel" />
      <el-table-column label="客户来源" align="center" prop="clientSource">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.travel_client_source" :value="scope.row.clientSource" />
        </template>
      </el-table-column>
      <el-table-column prop="orderCount" label="订单数" align="center">
        <template #default="scope">
          <el-link type="primary" @click="handleOrder(scope.row.clientId)" v-if="scope.row.orderCount!=0">
            {{ scope.row.orderCount }}
          </el-link>
          <el-link type="info" v-else>
            {{ scope.row.orderCount }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="行程数" align="center" prop="scheduleCount">
        <template #default="scope">
          <el-link type="primary" @click="handleSchedule(scope.row.clientId)" v-if="scope.row.scheduleCount!=0">
            {{ scope.row.scheduleCount }}
          </el-link>
          <el-link type="info" v-else>
            {{ scope.row.scheduleCount }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['travel:client:edit']">修改</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['travel:client:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改客户管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="clientName">
          <el-input v-model="form.clientName" placeholder="请输入客户姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="clientTel">
          <el-input v-model="form.clientTel" placeholder="请输入客户手机号" />
        </el-form-item>
        <el-form-item label="客户来源" prop="clientSource">
          <el-select v-model="form.clientSource" placeholder="请选择客户来源" style="width: 380px;">
            <el-option v-for="dict in dict.type.travel_client_source" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listClient, getClient, delClient, addClient, updateClient } from "@/api/travel/client";

export default {
  name: "Client",
  dicts: ['travel_client_source'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户管理表格数据
      clientList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clientName: null,
        clientTel: null,
        clientSource: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        clientName: [
          { required: true, message: "客户姓名不能为空", trigger: "blur" }
        ],
        clientTel: [
          { required: true, message: "客户手机号不能为空", trigger: "blur" }
        ],
        clientSource: [
          { required: true, message: "客户来源不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList()
  },
  methods: {

    /** 查询客户管理列表 */
    getList() {
      this.loading = true;
      listClient(this.queryParams).then(response => {
        this.clientList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log(response.rows);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        clientId: null,
        clientName: null,
        clientTel: null,
        clientSource: null,
        createBy: null,
        updateBy: null,
        createTime: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.clientId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const clientId = row.clientId || this.ids
      getClient(clientId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.clientId != null) {
            updateClient(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClient(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    handleOrder(row) {
      // 跳转到订单页面
      this.$router.push({
        path: '/travel/order',
        query: {
          clientId: row // 传递客户ID
        }
      });
    },
    handleSchedule(row) {
      console.log(row)
      // 跳转到行程页面
      this.$router.push({
        path: '/travel/schedule',
        query: {
          clientId: row // 传递客户ID
        }
      });
    }
  }
};
</script>
